import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from plotly.offline import init_notebook_mode
init_notebook_mode()
df = pd.read_csv('../data/capture_fisheries.csv', skiprows=4)
df = df.dropna(how='all')
df['Country Name'] = df['Country Name'].str.replace('"', '')
years = [str(year) for year in range(1960, 2025)]
id_vars = ["Country Name", "Country Code", "Indicator Name", "Indicator Code"]
df_melted = df.melt(id_vars=id_vars, value_vars=years,
var_name="Year", value_name="Production")
df_melted['Year'] = pd.to_numeric(df_melted['Year'])
df_melted['Production'] = pd.to_numeric(df_melted['Production'], errors='coerce')
df_melted = df_melted.dropna(subset=['Production'])
q95 = df_melted['Production'].quantile(0.95)
focused_data = df_melted[df_melted['Production'] <= q95]
zmax = focused_data['Production'].max()
zmin = focused_data['Production'].min()
enhanced_scale = [
[0.0, '#E1F5FE'],
[0.1, '#B3E5FC'],
[0.2, '#81D4FA'],
[0.3, '#4FC3F7'],
[0.4, '#29B6F6'],
[0.5, '#039BE5'],
[0.6, '#0288D1'],
[0.7, '#0277BD'],
[0.8, '#01579B'],
[0.9, '#0D47A1'],
[1.0, '#1d1c47']
]
total_production = df_melted.groupby(['Country Name', 'Country Code'])['Production'].sum().reset_index()
fig_total = px.choropleth(total_production,
locations="Country Code",
color="Production",
hover_name="Country Name",
color_continuous_scale=enhanced_scale,
range_color=[zmin, zmax],
labels={'Production': 'Production (metric tons)'},
projection="natural earth")
fig_total.update_layout(
width=700,
height=500,
geo=dict(
landcolor='#f0f0f0',
lakecolor='#d0e0f0',
bgcolor='white',
showframe=True,
framecolor='black'
),
coloraxis_colorbar=dict(
title="Production",
thickness=20,
len=0.75,
tickvals=np.linspace(zmin, zmax, 6),
ticktext=[f"{int(x):,}" for x in np.linspace(zmin, zmax, 6)]
),
margin={'t': 9, 'r': 20, 'b': 33, 'l': 18},
title={
'text': '<b>Total Fisheries Production per Country</b>',
'x': 0.42,
'xanchor': 'center',
'y': 0.95,
'yanchor': 'top',
'font': {
'color': '#0a2463',
}
}
)
fig_total.add_annotation(x=0.02, y=0.02,
xref="paper", yref="paper",
showarrow=False,
align='left',
xanchor='left', yanchor='bottom',
text="Total fisheries production per country since 1960.<br>" + \
'Hover over countries to see their specific production and drag to see different areas.<br>')
fig_total.update_annotations(
font=dict(color='#0a2463')
)
fig_total.show()